package expenager.prjct.two;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdapter {
	public static final String KEY_ROWID = "_id";
	public static final String KEY_EXPENSE = "expense";
	public static final String KEY_INCOME = "income";
	public static final String KEY_PAYTYPE = "paymentype";
	public static final String KEY_COMMENT = "comment";
	
	private static final String DATABASE_NAME="MyDB";
	private static final String DATABASE_Table="transaction";
	private static final int DATABASE_VERSION=1;
	
	private static final String DATABASE_CREATE = 
			"create table transaction (_id integer primary key autoincrement,"
			+ "name text not null, email text not null);";
					
	private final Context context;
	
	private DatabaseHelper DBHelper;
	private SQLiteDatabase db;
	
	public DBAdapter(Context ctx)
	{
		this.context = ctx;
		DBHelper = new DatabaseHelper(context);
	}
	
	private static class DatabaseHelper extends SQLiteOpenHelper
	{
		DatabaseHelper(Context context)
		{
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}
		
		@override
		public void OnCreate(SQLiteDatabase db)
		{
			try {
				db.execSQL(DATABASE_CREATE);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVwesion)
		{
			log.w(TAG, "Upgrading database from version " + oldversion + )
			"to" + newVersion + ",which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS contacts");
		}
	}
	
	
	
	 //---opens the database---
    public DBAdapter open() throws SQLException 
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    //---closes the database---
    public void close() 
    {
        DBHelper.close();
    }

    //---insert a contact into the database---
    public long insertexpense(int expense, int income, int paytype, int comment) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_EXPENSE, expense);
        initialValues.put(KEY_INCOME, income);
        initialValues.put(KEY_PAYTYPE, paytype);
        initialValues.put(KEY_COMMENT, comment);
        return db.insert(DATABASE_TABLE, null, initialValues);
    }

    //---deletes a particular contact---

    //---retrieves all the contacts---
    /*public Cursor getAllContacts()
    {
        return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME,
                KEY_EMAIL}, null, null, null, null, null);
    }
	*/
    //---retrieves a particular contact---
    public Cursor getCalories() throws SQLException 
    {
        Cursor mCursor =
                db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
                KEY_EXPENSE, KEY_INCOME, KEY_PAYTYPE, KEY_COMMENT},
                null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    //---updates a contact---
    public boolean updateTransaction(int expense,int income,int paytype, int comment) 
    {
        ContentValues args = new ContentValues();
        args.put(KEY_EXPENSE, expense);
        args.put(KEY_INCOME, income);
        args.put(KEY_PAYTYPE, paytype);
        args.put(KEY_COMMENT, comment);

        return db.update(DATABASE_TABLE, args, null, null) > 0;
    }


}